/**
 * Alibaba.com.cn Inc.
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.hiv.controller.wechat;

import com.hiv.common.BaseController;
import com.hiv.common.WebResponse;
import com.hiv.common.param.WechatLoginParam;
import com.hiv.mapper.domain.Admin;
import com.hiv.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhanghong
 * @version XcxUserController.java, v 0.1 2022/7/10 14:11 下午 XcxUserController Exp $$
 */
@Slf4j
@RestController
@RequestMapping("/wechat/user")
@Api(value = "小程序用户模块", tags = {"【小程序端】小程序用户模块"})
public class XcxUserController extends BaseController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "登录", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/login")
    public WebResponse<String> login(@RequestBody WechatLoginParam param) {
        return WebResponse.build(userService.login(param));
    }

    @ApiOperation(value = "退出登陆", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/logout")
    public WebResponse<Boolean> authLogout(HttpServletRequest request) {
        return WebResponse.build(logoutUser(request));
    }

    @ApiOperation(value = "获取个人信息", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/me")
    public WebResponse<Admin> me(HttpServletRequest request) {
        Admin admin = validUserLoginAndGet(request, Admin.class);
        return WebResponse.build(userService.getById(admin.getId()));
    }
}
